const fs = require('fs')

exports.get_test_data = function () {
  let content = fs.readFileSync('./mock/test.json', 'utf-8')
  return content
}

exports.get_index_data = function () {
  let content = fs.readFileSync('./mock/home.json', 'utf-8')
  return content
}

exports.get_chapter_data = function () {
  let content = fs.readFileSync('./mock/reader/chapter.json', 'utf-8')
  return content
}


exports.get_chapter_content_data = function (id) {
  if (!id) {
    id = 1
  }
  try {
    return fs.readFileSync(`./mock/reader/data/data${id}.json`, 'utf-8')
  } catch (error) {
    console.log(error)
  }
}

exports.get_rank_data = function () {
  return fs.readFileSync('./mock/rank.json')
}

exports.get_book_data = function (id) {
  if (!id) {
    id = 18218
  }
  try {
    return fs.readFileSync(`./mock/book/${id}.json`, 'utf-8')
  } catch (error) {
    console.log(error)
  }
}

exports.get_category_data = function () {
  try {
    return fs.readFileSync('./mock/category.json', 'utf-8')
  } catch (error) {
    console.log(error)
  }
}

exports.get_bookbacket_data = function () {
  try {
    return fs.readFileSync('./mock/bookbacket.json', 'utf-8')
  } catch (error) {
    console.log(error)
  }
}

exports.get_female_data = function() {
  try {
    return fs.readFileSync('./mock/channel/female.json', 'utf-8')
  } catch (error) {
    console.log(error)
  }
}

exports.get_male_data = function() {
  try {
   return fs.readFileSync('./mock/channel/male.json', 'utf-8')
  } catch (error) {
    console.log(error)
  }
}

exports.get_search_data = function(start, end, keyword) {
	return function(cb) {
		var http = require('http');
		var qs = require('querystring');
		var data = {
			s: keyword,
			start: start,
			end: end
		}
		var content = qs.stringify(data);
		var http_request = {
			hostname: 'dushu.xiaomi.com',
			port: 80,
			path: '/store/v0/lib/query/onebox?' + content,
			method: 'GET'
		};

		req_obj = http.request(http_request, function(_res) {
			var callback_content = '';
			var _this = this;
			var content='';
			_res.setEncoding('utf8');

			_res.on('data', function(chunk) {
				content += chunk;
			});

			_res.on('end', function(e) {
				cb(null,content);
			});

		});

		req_obj.on('error', function(e) {

		});

		req_obj.end();
	}
}

// 接口数据不对
// exports.get_search_data = function (start, end, keyword) {
//   return function (cb) {
//     let http = require('http')
//     let qs = require('querystring')

//     let data = {
//       s: keyword,
//       start: start,
//       end: end
//     }

//     let content = qs.stringify(data)

//     let http_request = {
//       hostname: 'www.duokan.com',
//       port: 80,
//       // path: '/store/v0/lib/query/onbox?' + content
//       path: '/store/v0/web/query/hint?' + content
//     }

//     req_obj = http.request(http_request, function (_res) {
//       let content = ''
//       _res.setEncoding('utf8')
//       _res.on('data', function (chunk) {
//         content += chunk
//       })
//       _res.on('end', function () {
//         cb(null, content)
//       })
//     })

//     req_obj.on('error', function () {})
//     req_obj.end()
//   }
// }
